package com.sptpc.pm2.ui.home;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;
import android.view.ViewGroup;


import androidx.annotation.NonNull;

import androidx.fragment.app.Fragment;

<<<<<<< HEAD
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
=======
>>>>>>> 0e895d52d486535db991a57c6bfa6dad0857729d
import androidx.viewbinding.ViewBinding;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sptpc.pm2.R;

import com.sptpc.pm2.databinding.FragmentHomeBinding;
import com.sptpc.pm2.ui.home.model.BannerAdapter;
import com.sptpc.pm2.ui.home.ui.BlankFragment;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment implements View.OnClickListener {
    private ViewBinding binding;
    private String []mTitles = {"每日贴士","邻里互助","居民热议"};
    private List<Fragment> fragments = new ArrayList<>();
    private SegmentTabLayout segmentTabLayout;
    private ViewPager viewPager;
    private BannerAdapter viewPagerAdapter;
    private ViewPager2 viewPager2;


    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       init();
       initFragment();
       initViewPager();
       initTab();
       initMst();
       return binding.getRoot();
    }

    private void initMst() {
        segmentTabLayout.showDot(0);
    }

    private void initTab() {
        segmentTabLayout.setTabData(mTitles);
        segmentTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    private void init(){
        binding = FragmentHomeBinding.inflate(getLayoutInflater());
        segmentTabLayout = binding.getRoot().findViewById(R.id.tb);
        viewPager = binding.getRoot().findViewById(R.id.vp);
        viewPagerAdapter = new BannerAdapter();
        viewPager2 = binding.getRoot().findViewById(R.id.info_Swiper);
        viewPager2.setAdapter(viewPagerAdapter);
    }

    @Override
    public void onClick(View v) {

    }

    private void initFragment()
    {
        for(int i = 0;i<mTitles.length;i++)
        {
            fragments.add(BlankFragment.newInstance(mTitles[i]));
        }
    }
    private void initViewPager(){
        viewPager.setAdapter(new MyFragmentAdapter(getChildFragmentManager()));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                segmentTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    class MyFragmentAdapter extends FragmentPagerAdapter {
        public MyFragmentAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        public MyFragmentAdapter(@NonNull FragmentManager fm, int behavior) {
            super(fm, behavior);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }


}

